﻿function fnDataTableAjax(selector, url, parametros, columns) {
    $.ajax({
        type: "POST",
        url: url,
        data: parametros,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            var data = $.parseJSON(response.d);
            var options = "";
            //console.log(response);
            var s = $(selector).dataTable({
                "bDestroy": true,
                "processing": true,
                "aaData": data,
                "aoColumns": columns
            });

            $(selector + ' tbody').on('click', 'tr', function () {

                if ($(this).hasClass('selected')) {
                    $(this).removeClass('selected');
                } else {
                    $(selector + ' tbody tr').removeClass('selected');
                    $(this).addClass('selected');
                }
            });

            //$('#example tbody tr').hover(function () {
            //    $(this).css('cursor', 'pointer');
            //}, function () {
            //    $(this).css('cursor', 'auto');
            //});

        },
        failure: function (msg) {
            alert(msg);
        }
    });
}


function fnLlenarSelect(selector, url, parametros, val, nom) {
    
    $.ajax({
        type: "POST",
        url: url,
        data: parametros,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {        
            var obj = JSON.parse(response.d);
            $(selector).find("option").remove();
            $.each(obj, function (i, item) {
                $(selector).append('<option value="' + eval(val) + '">' + eval(nom) + '</option>');
            });           
        },
        failure: function (msg) {
            alert(msg);
        }
    });
}


//Funciones auxiliares
$.fn.serializeObject = function () {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function () {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value;
        }
    });
    return o;
};

function fnRegistrarFormulario(url, parametros, success, error) {
    dat = JSON.stringify(parametros);    
    var rst = $.ajax({
        type: "POST",
        url: url,
        data: dat,
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    });
    
    if (typeof (success) != 'undefined' && success != null) {
        rst.done(function (data) {            
            success(JSON.parse(data.d));
        });       
    }
    
    if (typeof (error) != 'undefined' && error != null) {
        rst.fail(function (data) {
            console.log(data);
            error(JSON.parse(data.d));
        });        
    }
}

function fnMessageModal(mensaje) {
    $('#modalMessageContenido').html(mensaje);
    $('#modalMessage').modal({ show: true });    
}

$.fn.selectValue = function (value) {

};

function fnSelectValue(selector,value) {
    $(selector + " option[value=" + value + "]").attr("selected", true);
}

function fnEnviarSinParametros(url, success, error) {    
    var rst = $.ajax({
        type: "POST",
        url: url,
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    });
    
    if (typeof (success) != 'undefined' && success != null) {
        rst.done(function (data) {            
            success(JSON.parse(data.d));
        });       
    }
    
    if (typeof (error) != 'undefined' && error != null) {
        rst.fail(function (data) {            
            error(JSON.parse(data.d));
        });        
    }
}


